<?php
class item_othersignature extends template_item
{
	var $default_name = "Change Other Signatures";
	var $default_description = "Use this item to change other member's signatures";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['protected'] = array($this->ipsclass->vars['admin_group']);
		}
	}
	
	function admin_form()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $g_id => $group)
		{
			$groups[] = array($g_id,$group['g_title']);
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which groups are protected from this item?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][protected][]", $groups, $this->item_config['protected']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['protected'] = $input['protected'] ? $input['protected'] : array();
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
 		$this->ipsclass->han_editor->remove_side_panel = 1;
 		$this->ipsclass->han_editor->remove_emoticons  = 1;
        
        $editor = $this->ipsclass->han_editor->show_editor( '', 'signature' );
        
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<script type="text/javascript" src='jscripts/ipb_xhr_findnames.js'></script>
<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:1'></div>
<div class='formsubtitle'>{$this->ipsclass->lang['change_other_signature']}</div>
<form action='{$this->base_url}' method='post' id='changeform' onsubmit='return ValidateForm()'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['which_member']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' id='member_name' name='member_name' value='' size='30' class='forminput' autocomplete='off' style='width:210px' />
			</td>
		</tr>
		<tr>
			<td class='row1' align='left' colspan='2'>
{$editor}
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['change']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	// INIT find names
	init_js( 'changeform', 'member_name');
	// Run main loop
	setTimeout( 'main_loop()', 10 );
</script>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['member_name']) && isset($this->ipsclass->input['signature']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		if(!$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_user'));
		}
		
		if(in_array($member['mgroup'],$this->item_config['protected']))
		{
			$this->ipsclass->Error(array('MSG'=>'protected_user'));
		}
		
		$this->ipsclass->input['signature'] = $this->ipsclass->han_editor->process_raw_post('signature');
		
		$this->ipsclass->parser->parse_smilies     = 0;
		$this->ipsclass->parser->parse_html        = intval($this->ipsclass->vars['sig_allow_html']);
		$this->ipsclass->parser->parse_bbcode      = intval($this->ipsclass->vars['sig_allow_ibc']);
		$this->ipsclass->parser->parsing_signature = 1;
		
		$this->ipsclass->input['signature']        = $this->ipsclass->parser->bad_words( $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->input['signature'] ) ) );
		
		if ($this->ipsclass->parser->error != "")
		{
			$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => $this->ipsclass->parser->error) );
		}
		
		$this->ipsclass->DB->force_data_type = array( 'signature' => 'string' );
		if ( $member = $this->ipsclass->DB->simple_exec_query( array( 'select' => 'id', 'from' => 'member_extra', 'where' => 'id='.$this->ipsclass->member['id'] ) ) )
		{
			$this->ipsclass->DB->do_update('member_extra',array(
																	'signature' => $this->ipsclass->input['signature']
											),"id=".$member['id']);
		}
		else
		{
			$this->ipsclass->DB->do_update('member_extra',array(
																	'id'        => $member['id'],
																	'signature' => $this->ipsclass->input['signature']
											)					);
		}
		
		if ( USE_MODULES == 1 )
 		{
			require ROOT_PATH."modules/ipb_member_sync.php";
			$modules = new ipb_member_sync();
			$modules->register_class(&$this);
     		$modules->on_signature_update($member, $this->ipsclass->input['signature']);
    	}
    	
		$this->ipsclass->lang['other_member_signature_changed'] = str_replace("<#NAME#>",$member['members_display_name'],$this->ipsclass->lang['other_member_signature_changed']);
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Changed <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a> signature");
		
		$this->ipsclass->market_class->redirect('other_member_signature_changed');
	}
}
?>